/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxConfigForHandler
 * @Package com.rx.core.bean
 * @Description: 处理器配置
 * @author: 陈锦韬
 * @date: 2021\6\12 0012
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.bean;

import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.enm.RX_LOG_MODE;
import com.rx.core.util.EmptyChecker;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: Administrator
 * @Description: 处理器配置
 * @date: 2021\6\12 0012
 */
@Configuration
@ConfigurationProperties(prefix="com.rx.handler")
@Data
public class RxConfigForHandler {
    public static final String URL = "url";
    public static final String EX_URL = "exUrl";
    public static final String NAME = "name";
    public static final String ORDER = "order";
    public static final String TYPE = "type";
    public static final String EX_TYPE = "exType";
    public static final String STOP = "stop";

    /**
     * 前处理器配置
     */
    private List<RxConfigHandlerBean> prev;
    /**
     * 覆盖处理器配置
     */
    private List<RxConfigHandlerBean> over;
    /**
     * 后处理器配置
     */
    private List<RxConfigHandlerBean> after;

    @Data
    @Slf4j
    public static class RxConfigHandlerBean {
        public String url;
        public String exUrl = "";
        public String name = "";
        public String order = "0";
        public String type = "";
        public String exType = "";
        public String stop;

        public boolean noName(){
            if (EmptyChecker.isEmpty(name)){
                log.warn(RX_LOG_MODE.ERROR.getFormat("配置错误，没有指定处理器"));
                return true;
            }
            return false;
        }
        public boolean yesStop(){
            if (EmptyChecker.isEmpty(stop)){
                return false;
            }
            log.info(RX_LOG_MODE.DEFAULT.getFormat("{}"),"移除处理器",name);
            return true;
        }
        private List<HANDLER_TYPE> getEnumHandlerTypeByString(String types){
            List<HANDLER_TYPE> list = new ArrayList<>();
            String[] splitTypes = types.split(",");
            for (String splitType : splitTypes) {
                HANDLER_TYPE handlerType = HANDLER_TYPE.enumByName(splitType.trim());
                if (EmptyChecker.notEmpty(handlerType)){
                    list.add(handlerType);
                }
            }
            return list;
        }
        public List<HANDLER_TYPE> getEnumHandlerTypeList(){
            if(EmptyChecker.isEmpty(type)){
                return Collections.emptyList();
            }
            return getEnumHandlerTypeByString(type);
        }
        public List<HANDLER_TYPE> getExHandlerTypeList(){
            if(EmptyChecker.isEmpty(exType)){
                return Collections.emptyList();
            }
            return HANDLER_TYPE.getExTypeList(getEnumHandlerTypeByString(exType));
        }
        private List<String> getUrlListByString(String urls){
            String[] urlArray = urls.split(",");
            List<String> list = new ArrayList<>();
            for (String url : urlArray) {
                list.add(url.trim());
            }
            return list;
        }
        public List<String> getUrlList(){
            if(EmptyChecker.isEmpty(url)){
                // 空表示适用于所有
                return Collections.emptyList();
            }
            return getUrlListByString(url);
        }
        public List<String> getExUrlList(){
            if(EmptyChecker.isEmpty(exUrl)){
                // 空表示适用于所有
                return Collections.emptyList();
            }
            return getUrlListByString(exUrl);
        }
        public Integer dealOrder(){
            if (EmptyChecker.isEmpty(order)){
                order = "0";
                // 默认等级
            }
            return Integer.valueOf(order);
        }
    }

}
